package cn.wolfcode.plus.web.controller;

import cn.wolfcode.plus.domain.Employee;
import cn.wolfcode.plus.qo.PageResult;
import cn.wolfcode.plus.qo.QueryObject;
import cn.wolfcode.plus.service.IDepartmentService;
import cn.wolfcode.plus.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Author wyy
 * Date 2021/2/10 11:42
 **/
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    //@ResponseBody
    public String list(Model model, QueryObject qo){
        PageResult<Employee> result = employeeService.query(qo);
        model.addAttribute("result",result);
        return "employee/list";
        //return "login";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        //System.out.println(id);
        employeeService.removeById(id);
        return "redirect:/employee/list";
    }

//    @RequestMapping("/input")
//    public String input(Long id,Model model){
//        List<Department> departments = departmentService.listAll();
//        model.addAttribute("departments",departments);
//        List<Role> roles = roleService.listRoles();
//        model.addAttribute("roles",roles);
//        if(id!=null){
//            //去修改
//            List<Role> rolesForId = employeeService.queryRoles(id);
//            model.addAttribute("rolesForId",rolesForId);
//            Employee employee = employeeService.get(id);
//            model.addAttribute("employee",employee);
//        }
//        return "/employee/input";
//    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee){
        if(employee.getId()==null){
            //执行增加操作
            employeeService.save(employee);
        }else{
            //执行修改操作
            employeeService.updateById(employee);
        }
        return "redirect:/employee/list";
    }
}
